﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data;
using Model;
using System.Collections.Generic;
using DAL;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //不让浏览器缓存
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        context.Response.ContentType = "text/plain";

        int pageindex;
        int.TryParse(context.Request["p"], out pageindex);
        int pagesize;
        int.TryParse(context.Request["s"], out pagesize);

        string type = "all";
        if (!string.IsNullOrEmpty(context.Request["cate"]))
        {
            type = context.Request["cate"].ToString();
        }

        if (pageindex == 0)
            pageindex = 1;
        if (pagesize == 0)
            pagesize = 8;

        List<Idea> list = null;
        IdeaService isc = new IdeaService();
        switch (type)
        {
            case "all":
                list = (List<Idea>)isc.GetIdeaListByAll(context.Request["u"], pagesize, pageindex);
                break;
            case "votes":
                list = (List<Idea>)isc.GetIdeaListByVotes(context.Request["u"], pagesize, pageindex);
                break;
            case "user":
                list = (List<Idea>)isc.GetIdeaListByUser(context.Request["u"], pagesize, pageindex);
                break;
            case "favour":
                list = (List<Idea>)isc.GetIdeaListByFavourite(context.Request["u"], pagesize, pageindex);
                break;
        }
        string jsonData = JsonHelper.ObjectToJSON(list, "Ideas");
        context.Response.Write(jsonData);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}